<?php
    
    namespace App\Http\Controllers\Classify;
    
    use App\Models\Attribute;
    use App\Models\AttributeClassify;
    use App\Models\Classify;
    use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;
    
    class ClassifyController extends Controller
    {
       
        public function index()
        {
            
            $classifys = Classify::paginate(12);
            return view('classify.classify.index', compact('classifys'));
        }
        
        public function create()
        {
            $attrs = Attribute::where('pid', 0)->get();
            $classifys = Classify::where('level','<',2)->get();
            return view('classify.classify.create', compact('attrs', 'classifys'));
        }
        
        /**
         * Store a newly created resource in storage.
         *
         * @param  \Illuminate\Http\Request $request
         * @return \Illuminate\Http\Response
         */
        public function store(Request $request)
        {
            
//            dd($request->toArray());
            $request->validate([
                'name'=>'required',
                'pid'=>'required'
            ],[
                'name.required'=>'分类名称不能为空',
                'pid.required'=>'分类级别不能为空',
            ]);
//            找到新添加的分类父级的父级数据
            $class = Classify::where('id', $request->pid)->first();
            if ($request->pid == 0) {
                $request['level'] = 1;
              $classify= Classify::create($request->all());
            }elseif ($request->pid != 0 && $class->pid == 0){
                $request['level'] = 2;
               $classify=Classify::create($request->all());
            }elseif($request->pid != 0 && $class->pid != 0){
                $request['level'] = 3;
              $classify=Classify::create($request->all());
            }
            if($request->attribute){
                foreach ($request->attribute as $item) {
                    AttributeClassify::create([
                        'attribute_id' => $item,
                        'classify_id' => $classify->id,
                    ]);
                }
            }
            return back();
//            return redirect()->route('classify.classify.index')->with('success','添加分类成功');
            
        }
        /**
         * Display the specified resource.
         *
         * @param  \App\Models\Classify $classify
         * @return \Illuminate\Http\Response
         */
        public function show(Classify $classify)
        {
            //
        }
        
        /**
         * Show the form for editing the specified resource.
         *
         * @param  \App\Models\Classify $classify
         * @return \Illuminate\Http\Response
         */
        public function edit(Classify $classify)
        {
           $classifys=Classify::where('pid',0)->get();
//           获取所有属性
            $attributes=Attribute::get();
            //获取此分类对应日的属性有哪些
            $data=AttributeClassify::where('classify_id',$classify->id)->pluck('attribute_id');
           $attrId=$data->toArray();
            
            return view('classify.classify.edit', compact('classify','classifys','attributes','attrId'));
        }
        public function update(Request $request, Classify $classify)
        {
            $request->validate([
                'name'=>'required',
                'pid'=>'required'
            ],[
                'name.required'=>'分类名称不能为空',
                'pid.required'=>'分类级别不能为空',
            ]);
            $pid=intval($request['pid']);
            if($pid!=0){
                $class = Classify::where('id', $pid)->first();
            }
            if($pid==$classify->pid){
                $request['level']=$classify->level;
               $classify->update($request->all());
            }else{
                if ($pid== 0) {
                    $request['level'] = 1;
                    $classify= $classify->update($request->all());
                }elseif ($request->pid != 0 && $class->pid == 0){
                    $request['level'] = 2;
                    $classify=$classify->update($request->all());
                }elseif($request->pid != 0 && $class->pid != 0){
                    $request['level'] = 3;
                    $classify=$classify->update($request->all());
                }
            }
            if($request->attribute){
                $attId=AttributeClassify::where('classify_id',$classify->id)->pluck('attribute_id');
                foreach ($request->attribute as $item) {
                    if (in_array($item,$attId->toArray())){
                       $att_clss=AttributeClassify::where('attribute_id',$item)->first();
                        $att_clss->update(['attribute_id'=>$item]);
                    }else{
                        AttributeClassify::create([
                        'attribute_id' => $item,
                        'classify_id' => $classify->id,
                    ]);
                    }
                }
            }
            return redirect()->route('classify.classify.index')->with('success', '编辑分类成功');
        }
        public function destroy(Classify $classify)
        {
         
            $id=AttributeClassify::where('classify_id',$classify->id)->first();
            
            if ($id){
                Attribute::where('id',$id)->delete();
            }
            $classify->delete();
            return back()->with('success', '删除成功');
        }
        //根据父级id找子级分类
        public function classifySon(Classify $classify){
//        dump($classify->id);
        $classifySon=Classify::where('pid',$classify->id)->get();
        return $classifySon;
        
        }
    }
